Import main

Global SpriteLineList:List<cSpriteLine> = New List<cSpriteLine>

Class cSpriteSegment
	Field _x:Float
	Field _y:Float
	Field _xx:Float
	Field _yy:Float
	Field _id:String
	Field _length:Float
	Field _angle:Float
	Field _line:GameImage
	Field _linestart:GameImage
	Field _lineend:GameImage
	Field _alpha:Float
	
	Method New(_x:Float, _y:Float, _xx:Float, _yy:Float, _id:String)
		Self._x = _x
		Self._y = _y
		Self._xx = _xx
		Self._yy = _yy
		Self._id = _id
		Self._alpha = 1
		
		Self.setangle()
		Self.setlinelength()
		
		Self._line = diddyGame.images.Find(_id)
		Self._linestart = diddyGame.images.Find(_id + "_start")
		Self._lineend = diddyGame.images.Find(_id + "_end")
		
		'this should be dynamic, grab the image height and / 2
		Self._linestart.image.SetHandle(5, 5)
		Self._line.image.SetHandle(0, 5)
		Self._lineend.image.SetHandle(0, Self._lineend.image.Width)
		
		'SpriteLineList.AddLast(Self)
	End Method

	Method Draw()
	
		SetAlpha Self._alpha
		cDrawSpriteSegment(Self._line.image, Self._x, Self._y, Self._angle, Self._length / Self._line.image.Width, 0.8)
		SetAlpha 1
		
	End Method
	
	Method Update(_speed:Float = 1)
		Self._alpha -= (0.005 * _speed)
	End Method
	
	Method setangle()
		Self._angle = ATan2( (_xx - _x), (_yy - _y)) - 90
	End Method
	
	Method setlinelength()
		
		Local dx:Float = Self._x - Self._xx
		Local dy:Float = Self._y - Self._yy
		
		Self._length = Sqrt(dx * dx + dy * dy)
		
	End Method
		
End Class



Class cSpriteLine
	Field cSegmentList:List<cSpriteSegment> = New List<cSpriteSegment>
	Field _seg:cSpriteSegment
	Field _x:Float
	Field _y:Float
	Field _type:Int
	Field _drawTime:Int = 0
	Field _drawInt:Int = 1000
	Field _segmentLength:Int
	
	Field _velx:Float
	Field _vely:Float
	Field _angle:Int
	Field _speed:Float
		
	Method New(_x:Float, _y:Float, _type = 2)
		
		Self._angle = Rnd(360)
		Self._velx = Sin(Self._angle)
		Self._vely = Cos(Self._angle)
		Self._speed = Rnd(2, 6)
		
		Self._x = _x
		Self._y = _y
		Self._type = _type
		
		Select Self._type
			Case 1	
				Self._seg = New cSpriteSegment(Self._x, Self._y, Self._x, Self._y, "red_line")
			Case 2
				Self._seg = New cSpriteSegment(Self._x, Self._y, Self._x, Self._y, "green_line")
		End Select
		
		
		
		
		Self._drawTime = Millisecs()
		Self.cSegmentList.AddLast(Self._seg)
		SpriteLineList.AddLast(Self)
			
	End Method

	Method Update()
		
		If Self._x < 0 Then
			Self._velx = -Self._velx
			
		EndIf

		If Self._x > 640 Then
			Self._velx = -Self._velx
		EndIf
		
		If Self._y < 0 Then
			Self._vely = -Self._vely
		EndIf

		If Self._y > 480 Then
			Self._vely = -Self._vely
		EndIf
				
		
		Select Self._type
			Case 1
				Self._x += Self._velx * Self._speed
				Self._y += Self._vely * Self._speed
			Case 2
				If MouseDown(2)
					Self._x=MouseX()
					Self._y=MouseY()
				Else
					Self._x += Self._velx * Self._speed
					Self._y += Self._vely * Self._speed				
				End if
		End select
		
		
		If Self.cSegmentList.Count() > 0
		
			For Local s:cSpriteSegment = EachIn Self.cSegmentList
			
				
				
				Local ctime = Millisecs()
					
				Local tline:cSpriteSegment = Self.cSegmentList.Last()
				tline.setlinelength()
				tline.setangle()
				
				If ( (ctime - Self._drawTime) > Self._drawInt) Then
				
					Self._drawTime = Millisecs()
					
					
					Select Self._type
						Case 1	
							Local tline:cSpriteSegment = New cSpriteSegment(tline._xx, tline._yy, Self._x, Self._y, "red_line")
							Self.cSegmentList.AddLast(tline)
						Case 2
							Local tline:cSpriteSegment = New cSpriteSegment(tline._xx, tline._yy, Self._x, Self._y, "green_line")
							Self.cSegmentList.AddLast(tline)
					End Select					
					
					
				Else
					'draw a line from the last seg to the current emitter.
					If (tline._length > 10)
						Self._drawTime = Millisecs()
						
						Select Self._type
							Case 1	
								Local tline:cSpriteSegment = New cSpriteSegment(tline._xx, tline._yy, Self._x, Self._y, "red_line")
								Self.cSegmentList.AddLast(tline)
							Case 2
								Local tline:cSpriteSegment = New cSpriteSegment(tline._xx, tline._yy, Self._x, Self._y, "green_line")
								Self.cSegmentList.AddLast(tline)
						End Select	
						
									
					Else
						cSegmentList.Last._xx = Self._x
						cSegmentList.Last._yy = Self._y
					Endif
				Endif
				
				s.Update(Self._speed)
				
				If s._alpha <= 0 Then Self.cSegmentList.RemoveEach(s)
				
			Next
		Else
			SpriteLineList.RemoveEach(Self)
		EndIf
			
			
		
	End 
	
	Method Draw()
		
		'DrawRect(Self._x, Self._y, 2, 2)
		
		For Local s:cSpriteSegment = EachIn Self.cSegmentList
			s.Draw()
		Next
	End Method
	
End Class


Function cDrawSpriteSegment:Void(_line:Image, _x:float, _y:float, _angle:int, _length:float, _thickness:float)
	DrawImage(_line, _x, _y, _angle, _length, _thickness)
End Function

Function cDrawSpriteLine:Void(_line:Image, _x:float, _y:float, _xx:Float, _yy:float, _thickness:float = 1)
	Local _angle:float = ATan2( (_xx - _x), (_yy - _y))
	_angle-=90
	Local dx:Float = _x - _xx
	Local dy:Float = _y - _yy
	Local _length:float = Sqrt(dx * dx + dy * dy)
	
	DrawImage(_line, _x, _y, _angle, _length, _thickness)
End Function

